package com.accp.vehiclecare.action.xzhaction;

import com.accp.vehiclecare.biz.xzhbiz.JuriBiz;
import com.accp.vehiclecare.biz.xzhbiz.JurisdictionBiz;
import com.accp.vehiclecare.biz.xzhbiz.RoleBiz;
import com.accp.vehiclecare.pojo.Jurisdiction;
import com.accp.vehiclecare.vo.xzhvo.FunctionVO;
import com.accp.vehiclecare.vo.xzhvo.JurisdictionVO;
import com.accp.vehiclecare.vo.xzhvo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/JurisdictionAction")
public class JurisdictionAction {
    @Autowired
    private JurisdictionBiz jurisdictionBiz;
    @Autowired
    private RoleBiz roleBiz;

    @Autowired
    private JuriBiz juriBiz;

    /**
     * 查询父级权限下的子权限
     * @return
     */
    @GetMapping("/queryJurisdictionFunction")
    public List<JurisdictionVO> queryJurisdictionFunction(){
        return jurisdictionBiz.queryJurisdictionFunction();
    }

    /**
     * 查询角色
     * @return
     */
    @GetMapping("/queryRole")
    public List<RoleVO> queryRole(){
       return roleBiz.queryRole();
    }

    /**
     * 查询角色功能
     * @param jurisdictionid
     * @return
     */
    @GetMapping("/queryFunction/{jurisdictionid}")
    public List<FunctionVO> queryFunction(@PathVariable Integer jurisdictionid){
        return jurisdictionBiz.queryFunction(jurisdictionid);
    }

    /**
     * 新增角色权限
     * @param jurisdictionid
     * @param roleJurisdictions
     * @return
     */
    @PostMapping("/addJurisdiction/{jurisdictionid}")
    public Map<Object,String> addJurisdiction(@PathVariable Integer jurisdictionid,@RequestBody List<Integer> roleJurisdictions){
        Map<Object,String> map = new HashMap<Object,String>();
        try {
            jurisdictionBiz.addJurisdiction(jurisdictionid,roleJurisdictions);
            map.put("code","200");
            map.put("msg","ok");
        }catch (Exception exception){

            map.put("code","300");
            map.put("msg","on");
            throw new RuntimeException(exception);
        }
        return map;
    }

    /**
     * 查询角色列表
     * @return
     */
    @GetMapping("/queryJurisdiction")
    public List<Jurisdiction> queryJurisdiction(){
        return juriBiz.queryJurisdiction();
    }

    /**
     * 新增角色
     * @param jurisdiction
     * @return
     */
    @PostMapping("/addJurisdiction")
    public Map<Object,String> addJurisdiction(@RequestBody Jurisdiction jurisdiction){
        Map<Object,String> map = new HashMap<Object,String>();
        boolean bol = juriBiz.addJurisdiction(jurisdiction);
        if(bol){
            map.put("code","200");
            map.put("msg","ok");
        }else{
            map.put("code","300");
            map.put("msg","on");
        }
        return map;
    }

    /**
     * 修改角色
     * @param jurisdiction
     * @return
     */
    @PutMapping("/amendJurisdiction")
    public Map<Object,String> amendJurisdiction(@RequestBody Jurisdiction jurisdiction){
        Map<Object,String> map = new HashMap<Object,String>();
        boolean bol = juriBiz.amendJurisdiction(jurisdiction);
        if(bol){
            map.put("code","200");
            map.put("msg","ok");
        }else{
            map.put("code","300");
            map.put("msg","on");
        }
        return map;
    }


}
